package com.lijing.violenceMatch;

/**
 * @Description TODO
 * @title: ViolenceMatch
 * @Author LiJing
 * @Date: 2021/4/110:16 下午
 * @Version 1.0
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "w我是中国人 中国人 人";
        String str2 = "中国人";
        System.out.println(violenceMatch(str1,str2));
    }

    /**
     * 判断str1中包不包含str2
     * @param str1 str1
     * @param str2 str2
     * @return 返回匹配的索引，如果没有匹配的则返回-1
     */
    public static int violenceMatch(String str1, String str2){
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int s1Len = s1.length;
        int s2Len = s2.length;

        int i = 0;
        int j = 0;
        while (i < s1Len && j < s2Len){
            if (s1[i] == s2[j]){
                i++;
                j++;
            }else {
                i = i-(j-1);
                j = 0;
            }
        }
        if (j == s2Len){
            return i-j;
        }else {
            return -1;
        }
    }
}
